package com.smxy.daydayup.util.osa;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: yusy
 * @Date: 2022/8/12
 */
@RestController
public class OSAFetchController {

    public static final String FETCH_URI_PREFIX = "/api/osa/fetch/";
    private final SelfHelpFileFetcher selfHelpFileFetcher;

    public OSAFetchController(@Autowired(required = false) SelfHelpFileFetcher selfHelpFileFetcher) {
        this.selfHelpFileFetcher = selfHelpFileFetcher;
    }

    @GetMapping(FETCH_URI_PREFIX + "**")
    public void fetch(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (selfHelpFileFetcher == null) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        selfHelpFileFetcher.fetch(response, request.getRequestURI());
    }

}
